$(function () {
    init_addDialog()
});
/**初始化新增的数据验证**/
function init_addDialog() {
    /**初始化验证信息*/
    var icon = "<i class='zmdi zmdi-close-circle zmdi-hc-fw'></i> ";
    var  validator = $("#dept-add-form").validate({
        rules: {
            deptName: {
                required: true,
                minlength: 3,
                maxlength: 10,
                remote: {
                    url: "dept/checkDeptName",
                    type: "get",
                    dataType: "json",
                    data: {
                        deptName: function () {
                            return $("input[name='deptName']").val().trim();
                        },
                    }
                }
            }
        },
        messages: {
            deptName: {
                required: icon + "请输入部门名称",
                minlength: icon + "部门名称长度3到10个字符",
                remote: icon + "该部门名称已经存在"
            }
        }
    });
    /**初始化部门树状**/
    $.post(ctx + "dept/tree", {}, function (r) {
        if (r.code === 0) {
            var data = r.msg;
            $('#deptTree').jstree({
                "core": {
                    'data': data.children,
                    'multiple': false
                },
                "state": {
                    "disabled": true
                },
                "checkbox": {
                    "three_state": false
                },
                "plugins": ["wholerow", "checkbox"]
            });
        } else {
            $JuXue.n_danger(r.msg);
        }
    });
}
/**新增保存**/
function saveDeptInfo(){
    var ref = $('#deptTree').jstree(true);
    var deptIds = ref.get_checked();
    $("[name='parentId']").val(deptIds[0]);

    var $deptAddForm =  $("#dept-add-form");
    validator = $deptAddForm.validate();
    var flag = validator.form();
    if (flag) {
        $.post(ctx + "dept/add", $deptAddForm.serialize(), function (r) {
            if (r.code === 0) {
                dept_search();
                $("#dept_info").modal("hide");
                $JuXue.n_success(r.msg);
            } else $JuXue.n_danger(r.msg);
        });
    }else {
        $JuXue.n_warning("请填写完整信息");
    }
}

